<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">临沂市二级维护监管系统</h3>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>
      <el-row>
        <el-col :span="8">
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        </el-col>
        <el-col :span="8">
          <span @click="resetPassword" style="margin:0px 0px 25px 0px; color: #909399;font-size: 14px;cursor: pointer">维护站找回密码</span>
        </el-col>
        <el-col :span="8">
          <router-link class="link-type" :to="'/register'" style="margin:0px 0px 25px 0px;font-size: 14px;cursor: pointer">维护站注册</router-link>
        </el-col>
      </el-row>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <div class="center-line">
      <div class="center-line-top">临沂市二级维护监管系统</div>
      <div class="center-line-under">
        <p style="margin-left: 450px">----- 科技 智能 高效</p>
      </div>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2023</span>
    </div>
    <el-dialog
      title="维护站重置密码"
      :visible.sync="resetPasswordOpen"
      width="40%"
      :before-close="handleClose">
      <el-form ref="resetPasswordForm" :model="resetPasswordForm" :rules="resetPasswordFormRules" label-width="110px">
        <el-form-item label="手机号：" prop="phone">
          <el-input v-model="resetPasswordForm.phone"  placeholder="输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="短信验证码：" prop="code">
          <el-input style="width: 60%" v-model="resetPasswordForm.code" placeholder="输入验证码"></el-input>
          <span style="display: inline-block;background: #409EFF; color: #fff; text-align:center; width: 180px;height: 36px; margin-left: 20px" v-show="sendAuthCode" @click="getAuthCode">获取验证码</span>
          <span style="display: inline-block;background: #409EFF; color: #fff; text-align:center; width: 180px;height: 36px; margin-left: 20px;padding: 0 2px" v-show="!sendAuthCode"> <span class="auth_text_blue">{{auth_time}} </span> 秒之重新发送验证码</span>
        </el-form-item>
        <el-form-item label="新密码：" prop="password">
          <el-input  auto-complete="off" type="password" v-model="resetPasswordForm.password"  placeholder="输入新密码"></el-input>
          <span style="color: red">密码至少8位，包含数字、字母、英文大写、特殊字符</span>
        </el-form-item>
        <el-form-item label="确认密码："  prop="rePassword">
          <el-input   auto-complete="off" type="password" v-model="resetPasswordForm.rePassword" placeholder="输入确认密码"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetPasswordOpen = false">取 消</el-button>
        <el-button type="primary" @click="saveNewPassword">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'
import {getCode, resetPassword} from "../api/login";

export default {
  name: "Login",
  data() {
    return {
      auth_time: 0, /*倒计时 计数器*/
      sendAuthCode:true,/*布尔值，通过v-show控制显示‘获取按钮’还是‘倒计时’ */
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      resetPasswordOpen: false,
      resetPasswordForm:{},
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      resetPasswordFormRules: {
        phone: [
          { required: true, trigger: "blur", message: "请输入您的手机号" }
        ],
        code: [
          { required: true, trigger: "blur", message: "请输入验证码" }
        ],
        password: [{ required: true, trigger: "change", message: "请输入新密码" }],
        rePassword: [{ required: true, trigger: "change", message: "请输入确认密码" }]
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: true,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    saveNewPassword(){
      this.$refs.resetPasswordForm.validate(valid => {
        if(valid){
          resetPassword(this.resetPasswordForm).then(res => {
            if (res.code === 200){
              this.$message.success(res.msg);
              this.auth_time = 0;
              this.resetPasswordForm = {};
              this.resetPasswordOpen = false;
            }
          })
        }
      });
    },
    getAuthCode:function () {
      getCode(this.resetPasswordForm).then(res => {
        this.sendAuthCode = false;
        this.auth_time = 5*60;
        var auth_timetimer =  setInterval(()=>{
          this.auth_time--;
          if(this.auth_time<=0){
            this.sendAuthCode = true;
            clearInterval(auth_timetimer);
          }
        }, 1000);
      });
    },
    handleClose(){
      this.resetPasswordForm = {};
      this.resetPasswordOpen = false;
    },
    resetPassword(){
      this.resetPasswordOpen = true;
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/bg.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  position: fixed;
  z-index: 1;
  right: 100px;
  top: 340px;
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}

.center-line{
  //opacity: 0.6;
  background: rgb(52, 126, 188,0.6);
  width: 100%;
  height: 277px;
  position: absolute;
  top: calc(47vh - 118.5px);
}

.center-line-top{
  color: rgb(255, 255, 255);
  font-size: 70px;
  padding-top: 45px;
  margin-left: 15%;
  width: 80%;
}
.center-line-under{
  color: rgb(255, 255, 255);
  font-size: 19px;
  margin-top: 53px;
  margin-left: 15%;
  width: 80%;
}
</style>
